<template>
  <div>
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      :data="userList"
      border
      style="width: 100%"
    >
      <el-table-column fixed prop="name" label="昵称" width="250"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="250"></el-table-column>
      <el-table-column prop="sign" label="个性签名" width="250"></el-table-column>
      <el-table-column prop="setAdmin" label="设为管理" width="200">
        <el-switch
          slot-scope="scope"
          :value="scope.row.admin | setAdmin"
          @change="editAdmin(scope.row)"
        ></el-switch>
      </el-table-column>
      <el-table-column fixed prop="admin" label="身份" width="120">
        <template slot-scope="scope">{{ scope.row.admin | isAdmin }}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="modifyRow(scope.row)" type="text" size="small">修改</el-button>
          <el-button @click="deleteUser(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改用户信息" :visible.sync="dialogFormVisible">
      <el-form :model="modifyForm" :rules="rules" ref="modifyForm">
        <el-form-item label="昵称" prop="name">
          <el-input v-model="modifyForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="modifyForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="签名">
          <el-input v-model="modifyForm.sign" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="beforemodifyUser('modifyForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination
      background
      layout="prev, pager, next"
      :current-page="page"
      :total="count"
      :page-size="page_size"
      @current-change="handleCurrent"
    ></el-pagination>
  </div>
</template>

<script>
import {
  getUserList,
  setAdmin,
  deleteAdmin,
  modifyUser,
  deleteUser,
} from "@/api/adminSerivce";
export default {
  data() {
    return {
      dialogFormVisible: false,
      loading: true,
      userList: [],
      page: 1,
      page_size: 10,
      count: 0,
      formId: {
        id: "",
      },
      modifyForm: {
        id: "",
        email: "",
        name: "",
        sign: "",
      },
      rules: {
        name: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { type: "email", message: "请输入正确的邮箱地址", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    _getUserList() {
      getUserList(this.page, this.page_size).then((res) => {
        if (res.data.code === 200) {
          if (this.count == 0) {
            //只有查询第一页才返回总数
            this.count = res.data.data.count;
          }
          this.userList = res.data.data.data;
          this.loading = false;
        }
      });
    },
    handleCurrent(page) {
      this.page = page;
      this._getUserList();
    },
    modifyRow(row) {
      this.modifyForm.id = row.uid;
      this.modifyForm.name = row.name;
      this.modifyForm.email = row.email;
      this.modifyForm.sign = row.sign;
      this.dialogFormVisible = true;
    },
    beforemodifyUser(userForm) {
      this.$refs[userForm].validate((valid) => {
        if (valid) {
          this.modifyUser();
        } else {
          this.$message.error("请检查输入的数据");
          return;
        }
      });
    },
    modifyUser() {
      modifyUser(this.modifyForm)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success("修改成功");
            this.dialogFormVisible = false;
            this._getUserList();
          }
        })
        .catch((err) => {
          this.$message.error(err.response.data.msg);
        });
    },
    deleteUser(row) {
      this.formId.id = row.uid;
      deleteUser(this.formId).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("删除成功");
          //重新获取
          this._getUserList();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    editAdmin(row) {
      row.admin == 1 ? (row.admin = 0) : (row.admin = 1);
      this.formId.id = row.uid;
      if (row.admin == 1) {
        //设为管理
        setAdmin(this.formId)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success("设置成功");
            }
          })
          .catch((err) => {
            this.$message.error(err.response.data.msg);
            row.admin = 0;
          });
      } else {
        //删除管理
        deleteAdmin(this.formId)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success("移除成功");
            }
          })
          .catch((err) => {
            this.$message.error(err.response.data.msg);
            row.admin = 1;
          });
      }
    },
  },
  created() {
    this._getUserList();
  },
  filters: {
    isAdmin(admin) {
      if (admin == 1) {
        return "管理员";
      } else {
        return "普通用户";
      }
    },
    setAdmin(admin) {
      if (admin == 1) {
        return true;
      } else {
        return false;
      }
    },
  },
};
</script>
